﻿using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.IBLL;
using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.BLL
{
    public class UserManger : IUserManger
    {
        public OperResult<TBUsers> EditAvatar(TBUsers users)
        {
            IUersServer uersServer = new UersServer();
            try
            {
                TBUsers profileEntity = uersServer.EditAvatar(users);
                return OperResult<TBUsers>.Succeed(profileEntity);
            }
            catch (Exception ex)
            {
                return OperResult<TBUsers>.Failed(ex.ToString());
            }
        }
        #region EditWithoutAvatar 更新除头像以外的信息
        /// <summary>
        /// 更新除头像以外的信息
        /// </summary>
        /// <param name="Users"></param>
        /// <returns></returns>
        public OperResult<TBUsers> EditWithoutAvatar(TBUsers Users)
        {
            //实例化数据访问层 接口
            IUersServer userService = new UersServer();
            //try 如果无异常则跳过catch
            try
            {
                //使用EntityState更新数据库
                TBUsers profileEntity = userService.EditWithoutAvatar(Users);
                //返回处理结果
                return OperResult<TBUsers>.Succeed(profileEntity);
            }
            catch (Exception ex)
            {
                //直接返回错误
                return OperResult<TBUsers>.Wrong(ex);
            }
        }
        #endregion

        #region Reg 真正做添加
        /// <summary>
        /// 真正做添加
        /// </summary>
        /// <param name="tBLogins">获取的数据</param>
        /// <returns></returns> 
        public OperResult Reg(TBLogins tBLogins)
        {
            //查空
            if (string.IsNullOrWhiteSpace(tBLogins.UserName))
            {
                return OperResult.Failed("用户名不能为空");
            }
            if (string.IsNullOrWhiteSpace(tBLogins.MobileNumber))
            {
                return OperResult.Failed("手机号不能为空");
            }
            if (string.IsNullOrWhiteSpace(tBLogins.Password))
            {
                return OperResult.Failed("密码不能为空");
            }
            //手机号、账号查重
            IUersServer uersServer = new UersServer();
            if (uersServer.IsSomeUersName(tBLogins.UserName))
            {
                return OperResult.Failed("该用户名已注册");
            }
            if (uersServer.IsSomeMobile(tBLogins.MobileNumber))
            {
                return OperResult.Failed("该手机号已注册");
            }
            //密码加密
            SHAEncryption sHAEncryption = new SHAEncryption();
            tBLogins.Password = sHAEncryption.SHA1Encrypt(tBLogins.Password);
            //添加
            if (uersServer.AddUser(tBLogins))
            {
                return OperResult.Succeed();
            }
            else
            {
                return OperResult.Failed();
            }
        }
        #endregion
    }
}
